Skip to content

Conversation

@mgyoo86
Copy link
Contributor

@mgyoo86 mgyoo86 commented Dec 19, 2024

Summary: Add Macros to track and explain Expressions

This PR introduces two new macros (@store_expr and @explain) and related infrastructure to address the following idea from the code camp (see #786):

Backend/user interace

  • Show the expression that is being used to evaluate a certain field in dd

This PR provides a way to track the chained-executions of certain expressions, which can be helpful for debugging or understanding complex dynamically defined functions.

Dependency / Related PRs

This change depends on related PRs in IMAS and IMASdd packages to fully function.
Please refer to the following PR links in the related repositories:

Key Changes

  • Defines a new ExprInfo struct to store the name, arguments, and body of anonymous functions defined via dict["key"] = (args...) -> body assignments.
  • Introduces @store_expr macro:
    • Automatically executes the given assignment.
    • Extracts argument and body information from the anonymous function.
    • Stores the resulting ExprInfo in expr_info_dict.
  • Introduces @explain macro:
    • Temporarily enables IMASdd.FLAG_EXPLAIN_EXPRESSION.
    • Runs the provided expression under this flag to display additional explanatory information.
    • Resets the flag after execution.

Examples

1. Dynamic expressions

Suppose we want to execute the some dynamic expressions under dd.summary.volume_average as the following:
Screenshot 2024-12-19 at 1 55 44 PM

One can evaluate the expressions by typing it on the REPL, which will give the final result only.

# The following will give a single element Vector as the result
dd.summary.volume_average.n_e.value

Screenshot 2024-12-19 at 2 23 17 PM

However, to compute the final single value, the above expression actually executed multiple chained-expressions under the hood.

The new macro @explain explains these detailed procedures to evaluate the given dynamic expression. Note that it will also explain the chained expressions that are required to evalute the original expression.

# The following will explain the details of chained expressions
@explain dd.summary.volume_average.n_e.value; # add semicolon to omit the final result

image

2. Onetime expressions

One can use the same @explain macro to see the detailed procedure for the onetime expression as well.
Note that once the onetime expression is evaluated, it is no longer an expression and has a fixed value.
Hence, the subsequent call of @explain on the same onetime expression has nothing to explain.

# First execution will show the explanation
@explain dd.core_sources.source[2].profiles_1d[1].grid.area;

# Subsequent execution will show nothing, except the final result
@explain dd.core_sources.source[2].profiles_1d[1].grid.area;

Screenshot 2024-12-19 at 2 08 52 PM

Note

  • CI might fail until dependent PRs are merged.
  • Once the dependent changes are integrated, this PR should work as expected.

@bclyons12
Copy link
Member

@mgyoo86 What's the status of this? If it's good, please resolve the conflict and add the ready to merge label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants